package cn.com.customer.manager.ui.message.msg

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.com.customer.manager.Constants

import cn.com.customer.manager.R
import cn.com.customer.manager.adapter.MegListAdapter
import cn.com.customer.manager.base.BaseFragment
import cn.com.customer.manager.bean.*
import com.chad.library.adapter.base.BaseQuickAdapter
import com.xuexiang.xutil.data.DateUtils
import kotlinx.android.synthetic.main.fragment_message2.*

/**
 * A simple [Fragment] subclass.
 */
class MessageFragment : BaseFragment(), BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener,MessageContract.View {
    override fun getLayoutId(): Int = R.layout.fragment_message2
    private lateinit var mAdapter: MegListAdapter
    private var mPresenter:MessageContract.Presenter=MessagePresenter(this)
    private var requestParam: MutableMap<String, String> = mutableMapOf()
    private var list: MutableList<MsgPlayBean> = mutableListOf()
    private var type: Int = 0
    private var page: Int = 1
    override fun initView() {
        initRv()
        msg_tab.setDefaultSelection(0)
        msg_tab.setOnTabSelectionChangedListener { title, value ->
            type = when (title) {
                "未读" -> {
                    0
                }
                "已读" -> {
                    1
                }
                else -> {
                    2
                }
            }
            page=0
            requestData()
        }
        requestData()
    }

    private fun initRv() {
        msg_recycler.layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
        mAdapter = MegListAdapter(R.layout.item_message_list)
        msg_recycler.adapter = mAdapter
        mAdapter.onItemClickListener = this
        mAdapter.onItemChildClickListener = this
        mAdapter.setOnLoadMoreListener(this, msg_recycler)
    }

    override fun onLoadMoreRequested() {
        page++
        requestData()
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
         startActivity(Intent(activity, MessageDetailActivity::class.java).putExtra("data", list[position]))
    }
    companion object {

        @JvmStatic
        fun newInstance() = MessageFragment().apply {
            arguments = Bundle().apply {
            }
        }
    }
    private fun requestData() {
        requestParam["pageNo"]=page.toString()
        requestParam["pageSize"]=Constants.PAGE_SIZE.toString()
        requestParam["beginDate"]=""
        requestParam["endDate"]=DateUtils.getNowString(DateUtils.yyyyMMdd.get())
        mPresenter.getMessages(requestParam)
    }

    override fun setMessages(result: MsgListInfo) {
        when (type) {
            0 -> {
                result.receivedList.notReadList.forEach() {
                    list.add(MsgPlayBean(it,null))
                }
            }
            1 -> {
                result.receivedList.readList.forEach(){
                    list.add(MsgPlayBean(it,null))
                }
            }
            else -> {
                result.sendList.forEach(){
                    list.add(MsgPlayBean(null,it))
                }
            }
        }
        if (page == 1) {
            mAdapter.setNewData(list)
        } else {
            mAdapter.addData(list)
        }
        if (mAdapter.isLoadMoreEnable and mAdapter.isLoading) {
            mAdapter.loadMoreComplete()
        }
    }

    override fun setPresenter(presenter: MessageContract.Presenter) {
         mPresenter=presenter
    }
}
